//package hardy.etudejpa.config;
//
//import java.io.Serializable;
//import java.util.Arrays;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Map;
//
//import org.hardy.reflex.SpringReflext;
//import org.hibernate.CallbackException;
//import org.hibernate.EmptyInterceptor;
//import org.hibernate.EntityMode;
//import org.hibernate.Transaction;
//import org.hibernate.resource.jdbc.spi.StatementInspector;
//import org.hibernate.type.Type;
//
//import hardy.etudejpa.config.ShardingRegist.ThreadParam;
//import hardy.etudejpa.config.ShardingRegist.ThreadParamElement;
///**
// * -实现一个分表的hibernate拦截
// * @author hardy
// *
// */
//public class ShardingInspector extends EmptyInterceptor{
//	/**
//	 * 
//	 */
//	private static final long serialVersionUID = 1L;
//    
//	private ShardingRegist shardingRegist = ShardingRegist.getInstance();
//	
//	/**
//	 * -1.开始事务 第一步
//	 */
//	@Override
//	public void afterTransactionBegin(Transaction tx) {
//		System.out.println("=============>afterTransactionBegin");
//		super.afterTransactionBegin(tx);
//	}
//	/**
//	 * -2.获取实体名称
//	 */
//	@Override
//	public String getEntityName(Object object) {
//		System.out.println("=============>getEntityName");
//		return super.getEntityName(object);
//	}
//	
//	 /**
//     * -3.是否有事务
//     * (getEntityName)->(isTransient)->(onXXX)
//     */
//	@Override
//	public Boolean isTransient(Object entity) {
//		System.out.println("=============>isTransient");
//		return super.isTransient(entity);
//	}
//
//    /**
//     * -4. save
//     */
//	@Override
//	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
//		System.out.println("=============>onsave");
//		Object val = SpringReflext.getPropValue(entity, "phone"); 
//		ShardingRegist.getInstance().addNextThreadParam("onSave",ThreadParamElement.build("user_auth", hardy.etudejpa.config.ShardingRegist.Type.UPDATE).addParam("phone", val.toString()));
//	    return super.onSave(entity, id, state, propertyNames, types);
//	}
//	
//	
//
//	@Override
//	public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
//		System.out.println("=============>onDelete");
//		super.onDelete(entity, id, state, propertyNames, types);
//	}
//
//
//
//	@Override
//	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
//			String[] propertyNames, Type[] types) {
//		System.out.println("onFlushDirty=====>"+Thread.currentThread().getId());
//		return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
//	}
//
//
//
//	@Override
//	public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
//		System.out.println("=========>onLoad");
//		return super.onLoad(entity, id, state, propertyNames, types);
//	}
//
//	 
//	@Override
//	public void onCollectionRemove(Object collection, Serializable key) throws CallbackException {
//		System.out.println("=============>onCollectionRemove");
//		super.onCollectionRemove(collection, key);
//	}
//
//
//
//	@Override
//	public void onCollectionRecreate(Object collection, Serializable key) throws CallbackException {
//		System.out.println("=============>onCollectionRecreate");
//		super.onCollectionRecreate(collection, key);
//	}
//
//
//
//	@Override
//	public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException {
//		System.out.println("=============>onCollectionUpdate");
//		super.onCollectionUpdate(collection, key);
//	}
//
//	/**
//     * -在提交数据库之前  
//     * <br>  postFlush->(findDirty)->(onPrepareStatement)
//     */
//	@SuppressWarnings("rawtypes")
//	@Override
//	public void preFlush(Iterator entities) {System.out.println("=============>preFlush");}
//	
//	@Override
//	public int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
//			String[] propertyNames, Type[] types) { System.out.println("findDirty");
//		System.out.println("=============>findDirty");	
//		return null;
//	}
//
//	@Override
//	public String onPrepareStatement(String sql) { System.out.println("=============>onPrepareStatement=========>"+sql);
//		Map<String,List<String>> result = shardingRegist.getMappingTable(sql);
//	    if(result==null||result.isEmpty())return sql;
//	    else { //获取存在的分表
//	    	for(String tableVirtual:result.keySet()) {
//	    		if(result.get(tableVirtual).size()==1) { //只有一个分表
//		    		System.out.println("=======>"+result.get(tableVirtual));
//		    	}
//	    	}
//	    }
//	    return sql;
//	}
//	
//	
//	
//	/**
//     * -提交数据库 flush
//     * <br> (onPrepareStatement)->postFlush->beforeTransactionCompletion
//     */
//	@SuppressWarnings("rawtypes")
//	@Override
//	public void postFlush(Iterator entities) {
//		System.out.println("=============>postFlush");
//		shardingRegist.cleanThreadParam();
//		super.postFlush(entities);
//	}
//
//
//	@Override
//	public Object instantiate(String entityName, EntityMode entityMode, Serializable id) {
//		System.out.println("=============>instantiate");
//		return super.instantiate(entityName, entityMode, id);
//	}
//
//    /**
//     * -获取操作实体
//     * afterTransactionBegin->(getEntity)->(isTransient)
//     */
//	@Override
//	public Object getEntity(String entityName, Serializable id) {
//		System.out.println("=============>getEntity");
//		return super.getEntity(entityName, id);
//	}
//
//	@Override
//	public void afterTransactionCompletion(Transaction tx) {
//		System.out.println("=============>afterTransactionCompletion");
//		super.afterTransactionCompletion(tx);
//	}
//
//
//  /**
//   * - 在完成事务之前
//   * <br> postFlush->beforeTransactionCompletion->afterTransactionCompletion
//   */
//	@Override
//	public void beforeTransactionCompletion(Transaction tx) {
//		System.out.println("=============>beforeTransactionCompletion");
//		super.beforeTransactionCompletion(tx);
//	}
//	
//}
